//2009/08/29 20:07:24
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <sstream>
#include <algorithm>

using namespace std;

class RestaurantManager
{
public:
    int allocateTables(vector <int> tables, vector <int> groupSizes, vector <int> arr, vector <int> dep)
    {
		vector<int> v(tables.size(), 0); // To record the depart time of each table;
		sort(tables.begin(), tables.end());
		int ret = 0;
		for(int i=0; i<arr.size(); i++)
		{
			bool flag = false;
			for(int j=0; j<tables.size(); j++)
			{
				if(tables[j] >= groupSizes[i] && arr[i] >= v[j])
				{
					v[j] = dep[i];
					flag = true;
					break;
				}				
			}
			if(!flag) ret += groupSizes[i];
		}
		return ret;
    }
};
